package br.com.ravi.deposito.action;

import java.io.IOException;
import java.lang.reflect.Method;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import br.com.ravi.deposito.exceptions.NotInformedCommandException;

public class AbstractAction extends HttpServlet {
	private static final long serialVersionUID = 1641425234397792769L;
	
	@Override
	protected void service(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		String command = req.getParameter("command");
		
		if (command == null || command.length() == 0) {
			throw new NotInformedCommandException("Informe o comando para a Action");
		}
		
		try {
			Method method = this.getClass().getMethod(command, HttpServletRequest.class, HttpServletResponse.class);
			method.invoke(this, req, resp);
		} catch (Exception e) {
			throw new ServletException(e);
		}
	}
}